﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Globalization;
using System.Text.RegularExpressions;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MyConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double fontsize = 0;
            try
            {
                if (null == value)
                {
                    throw new NotImplementedException();
                }

                if (double.TryParse(value?.ToString(),out fontsize))
                 {
                    return fontsize;
                }
                else
                {
                    return ChineseNumberConverter.ChineseToArabic(value?.ToString());
                }
            }
            catch (Exception)
            {

            }


            switch (value)
            {
                case "二十":
                    return 20.0;
                default:
                    break;
            }

            return value;
            throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


    public class ChineseNumberConverter
    {
        private static readonly char[] chineseNumbers =
            new char[] { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' };
        private static readonly int[] numericValues =
            new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        public static int ChineseToArabic(string chineseNumber)
        {
            string pattern = string.Join("|", chineseNumbers);
            string[] parts = Regex.Split(chineseNumber, pattern);
            int result = 0;
            foreach (var part in parts)
            {
                if (part == "")
                    continue;

                int index = Array.IndexOf(chineseNumbers, part[0]);
                result = result * 10 + numericValues[index];
            }
            return result;
        }
    }
}